// pages/statistics/statistics.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orgname:'',
    orgurl:'',
    orgid:'',
    list:[],
    isorg:null,
    pageNumber:1,
    totalPages:1,
    username:'',
    imgurl:'',
  },
topages(e){
  var reportDate = e.currentTarget.dataset.reportdate
  var follow = e.currentTarget.dataset.follow
  wx.navigateTo({
    url: '/pages/statisticsDay/statisticsDay?orgid=' + this.data.orgid + '&reportdate=' + reportDate + '&isorg=' + this.data.isorg + '&follow=' + follow +'&orgname=' + this.data.orgname + '&orgurl=' + this.data.orgurl,
  })
},
  toexport(){
    wx.navigateTo({
      url: '/pages/export/export?orgid=' + this.data.orgid +'&isorg='+this.data.isorg,
    })
  },
  tosee(){
    var id = ''
    if(this.data.isorg==1){
      id=this.data.orgid
    }else if(this.data.isorg==0){
      id = wx.getStorageSync('uid')
    }
    console.log('isorg',this.data.isorg)
    wx.navigateTo({
      url: '/pages/epidemicSee/epidemicSee?isorg='+ this.data.isorg + '&id='+id,
    })
  },
  getimg(){
    var that = this
    app.req({
      url:'wechat/activity/getPictureListByType',
      data:{
        type: 25,
        pageSize: 10,
        pageNumber: 1
      },
      success:function(res){
        if(res.code==200){
          that.setData({
            imgurl:res.data.rows[0].url
          })
        }
      }
    })
  },
  getorg(){
    var that = this
    app.req({
      url: "orgmini/epidemic/orgPersonalStatisticsList",
      data: {
        orgId: that.data.orgid,
        pageNumber: that.data.pageNumber,
        pageSize: 10,
      },
      success: function (res) {
        if (200 == res.code) {
          if(that.data.pageNumber==1){
            that.setData({
              list: res.data.rows
            })
          }else{
            that.setData({
              list:that.data.list.concat(res.data.rows)
            })
          }
          that.setData({
            totalPages: res.data.totalPages
          })
        }
      }
    });
  },
  getpersonal(){
    var that = this
    app.req({
      noLoading: true,
      url: "orgmini/epidemic/personalStatisticsList",
      data: {
        pageNumber: that.data.pageNumber,
        pageSize: 10,
      },
      success: function (res) {
        if (200 == res.code) {
          if (that.data.pageNumber == 1) {
            that.setData({
              list: res.data.rows
            })
          } else {
            that.setData({
              list: that.data.list.concat(res.data.rows)
            })
          }
          that.setData({
            totalPages: res.data.totalPages
          })
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.setData({
        isorg:options.isorg
      })
    if (options.isorg==1){
      this.setData({
        orgid: options.orgid,
        orgname: options.orgname,
        orgurl: options.orgurl,
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    that.getimg()
    if(that.data.isorg==1){
      that.getorg()
    }else if(that.data.isorg==0){
      that.getpersonal()
    }
    app.getUserInfo(function (userInfo) {
      that.setData({
        username: userInfo.realName
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    if(that.data.totalPages>that.data.pageNumber){
      that.setData({
        pageNumber:that.data.pageNumber + 1
      })
      if (that.data.isorg == 1) {
        that.getorg()
      } else if (that.data.isorg == 0) {
        that.getpersonal()
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var title = ''
    var url = ''
    if (this.data.isorg == 1) {
      title = this.data.orgname +'邀请你进行平安打卡，免费领口罩'
      url = '/pages/orgHome/orgHome?id=' + this.data.orgid
    } else if (this.data.isorg == 0) {
      title = this.data.username +'邀请你进行平安打卡，免费领口罩'
      url = '/pages/epidemicSee/epidemicSee?isorg=' + this.data.isorg + '&id=' + wx.getStorageSync('uid')
    }
      return {
        title: title,
        imageUrl: this.data.imgurl,
        path: url
      }
  }
})